package service;

import java.io.ObjectInputStream.GetField;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.db.DBDriverFactory;
import org.entity.Student;

public class ExecuteService {

	// 登录界面
	public int longinScreen() {
		// 登录界面内容
		System.out.println("********用户登录*******");
		System.out.println("1.学生登录");
		System.out.println("2.老师登录");
		System.out.println("3.退出");
		System.out.println("*********************");
		// 登录方式选择，输入数字1/2/3选择登录方式
		Scanner sc = new Scanner(System.in);
		System.out.println("请选择登录方式：");
		int i = sc.nextInt();
		return i;
	}

	/**
	 * 学生登录系统
	 * 
	 * @param stuMessage
	 *            学生个人信息
	 * @return
	 */
	public Student studentlongin(Student stuMessage) {
		Scanner sc = new Scanner(System.in);
		while (true) {
			// 输入账号密码
			System.out.println("请输入账号：");
			String stuAccount = sc.next();
			System.out.println("请输入密码：");
			String stuPassword = sc.next();
			// 判断账号信息是否存在
			boolean isExists = isStuAccountExisted(stuAccount);
			if (isExists) {
				if (stuPassword.equals("888888")) {
					System.out.println("请修改密码");
					System.out.println("输入新密码：");
					String strpassword1 = sc.next();
					System.out.println("确认新密码：");
					String strpassword2 = sc.next();
					if (strpassword1.equals(strpassword2)) {
						String strSql = "update EDUMSTUDENTMESSAGE set studentpassword=" + strpassword1
								+ " where studentid=?";
						int j = DBDriverFactory.getDBDriverInstance().update(strSql, new Object[] { stuAccount });

						if (j > 0) {
							System.out.println("密码修改成功");
							// 将查询到的学生账号下的信息输入到学生类中
							List<Student> lst = getStudentInfoByCode(stuAccount);
							for (int n = 0; n < lst.size(); n++) {
								stuMessage = lst.get(n);
							}
							break;
						} else {
							System.out.println("密码修改失败，请重新登录");
							continue;
						}
					}

				}
				// 将查询到的学生账号下的信息输入到学生类中
				List<Student> lst = getStudentInfoByCode(stuAccount);
				for (int n = 0; n < lst.size(); n++) {
					stuMessage = lst.get(n);
				}

				// 如果用户存在
				// 判断密码是否正确
				if (stuPassword.equals(stuMessage.getStudentpassword())) {
					break;
				} else {
					System.out.println("密码错误，请重新输入");
					continue;
				}
			}
		}
		return stuMessage;
	}

	/**
	 * 老师登录系统
	 * 
	 * @return
	 */
	public void teacherlongin() {
		Scanner sc = new Scanner(System.in);
		while (true) {
			System.out.println("请输入账号：");
			String teaAccount = sc.next();
			System.out.println("请输入密码：");
			String teaPassword = sc.next();
			if (teaAccount.equals("admin") && teaPassword.equals("123456")) {
				break;
			}
			System.out.println("输入错误，请重新输入");
		}
	}

	// 老师功能菜单
	public void teacherFunctionMenu() {
		while (true) {
			System.out.println("*********************");
			System.out.println("*  1.添加学生信息");
			System.out.println("*  2.查询学生信息");
			System.out.println("*  3.修改学生信息");
			System.out.println("*  4.删除学生信息");
			System.out.println("*  0.返回上一层");
			System.out.println("*********************");
			System.out.println("请选择功能菜单：1,2,3,4,0");
			Scanner sc = new Scanner(System.in);
			int i = sc.nextInt();

			if (i == 1) {
				teaAddStuMessage();
			}
			if (i == 2) {
				teaSelectStuMessage();
			}
			if (i == 3) {
				teaUpdateStuMessage();
			}
			if (i == 4) {
				teaDeleteStuMessage();
			}
			if (i == 0) {
				// 退出
				System.out.println("结束操作");

				break;
			}
		}
	}

	// 学生功能菜单
	public void studentFunctionMenu(Student stuMessage) {
		while (true) {
			System.out.println("*********************");
			System.out.println("*  1.查询个人信息");
			System.out.println("*  2.修改密码");
			System.out.println("*  3.查询同学信息");
			System.out.println("*  4.添加好友");
			System.out.println("*  5.显示我的所有好友信息");
			System.out.println("*  (该功能暂未开放)7.给好友留言");
			System.out.println("*  (该功能暂未开放)8.查看我的留言板");
			System.out.println("*  (该功能暂未开放)9.删除好友");
			System.out.println("*  0.任意键退出");
			System.out.println("*********************");
			System.out.println("请选择功能菜单：1,2,3,4,5,6,7,8,9,10,0");
			Scanner sc = new Scanner(System.in);
			int i = sc.nextInt();

			if (i == 1) {
				stuSelectStuselfMessage(stuMessage);
			}
			if (i == 2) {
				stuUpdateStuMessage(stuMessage);
			}
			if (i == 3) {
				stuSelectStuMessage(stuMessage);
			}
			if (i == 4) {
				stuAddStuFriend(stuMessage);
			}
			if (i == 5) {
				stuSelectStuFriendsMessage(stuMessage);
			}
			if (i == 6) {

			}
			if (i == 0) {
				// 退出
				break;
			}
		}
	}

	/**
	 * 老师添加学生信息
	 */
	public void teaAddStuMessage() {
		Scanner sc = new Scanner(System.in);
		// 输入学生信息
		System.out.println("输入学生账号ID");
		Integer studentid = sc.nextInt();
		// System.out.println("输入密码");
		// Integer studentpassword = sc.nextInt();
		System.out.println("输入学生姓名");
		String studentname = sc.next();
		System.out.println("输入学生性别");
		String studentsex = sc.next();
		System.out.println("输入学生年龄");
		Integer studentage = sc.nextInt();
		System.out.println("输入学生电话");
		Long studentphonenumber = sc.nextLong();
		System.out.println("输入学生家庭住址");
		String studentaddress = sc.next();
		System.out.println("输入学生QQ号");
		Long studentqq = sc.nextLong();

		Object[] params = new Object[] { studentid, studentname, studentsex, studentage, studentphonenumber,
				studentaddress, studentqq };

		String strSql = "insert into EDUMSTUDENTMESSAGE(studentid,"
				+ "studentname,studentsex,studentage,studentphonenumber,"
				+ "studentaddress,studentqq) values(?,?,?,?,?,?,?)";

		int i = DBDriverFactory.getDBDriverInstance().update(strSql, params);
		if (i > 0) {
			System.out.println("信息录入成功");
			List lst = getStudentInfoByCode(String.valueOf(studentid));
			printList(lst);
		} else {
			System.out.println("信息录入失败");
		}
	}

	/**
	 * 老师删除学生信息（逻辑删除）
	 * 
	 * @param stuAccount
	 */
	public void teaDeleteStuMessage() {
		Scanner sc = new Scanner(System.in);
		// 输入学生账号ID
		System.out.println("输入要删除的学生账号ID");
		Integer studentid = sc.nextInt();
		String strSql = "update EDUMSTUDENTMESSAGE set studentcondition=0 where studentid=?";
		int i = DBDriverFactory.getDBDriverInstance().update(strSql, new Object[] { studentid });

		if (i > 0) {
			System.out.println("信息录入成功");
			List lst = getStudentInfoByCode(String.valueOf(studentid));
			printList(lst);
		} else {
			System.out.println("信息录入失败");
		}
	}

	/**
	 * 修改学生信息
	 * 
	 * @param stuAccount
	 *            要修改的
	 */
	public void teaUpdateStuMessage() {
		// 显示学生信息
		String strSql = "select * from EDUMSTUDENTMESSAGE";
		List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<Student>(Student.class));
		printList(lst);

		Scanner sc = new Scanner(System.in);
		// 输入学生账号ID
		System.out.println("输入要修改的学生账号ID");
		Integer studentid = sc.nextInt();
		// 选择要修改的字段
		System.out.println("请输入要修改的字段：");
		String strColumn = sc.next();
		System.out.println("输入修改值：");
		String strValue = sc.next();

		String strSql1 = "update EDUMSTUDENTMESSAGE set " + strColumn + "=" + strValue + " where studentid=?";
		int i = DBDriverFactory.getDBDriverInstance().update(strSql1, new Object[] { studentid });

		if (i > 0) {
			System.out.println("信息录入成功");
			List lst1 = getStudentInfoByCode(String.valueOf(studentid));
			printList(lst1);
		} else {
			System.out.println("信息录入失败");
		}
	}

	/**
	 * 查询学生信息
	 * 
	 * @param stuAccount
	 */
	public void teaSelectStuMessage() {
		System.out.println("选择要查询的内容：");
		System.out.println("1.查询所有学生信息");
		System.out.println("2.根据指定编号查询单个学生信息");
		System.out.println("3.根据姓名模糊查询学生信息");
		System.out.println("4.根据地址模糊查询学生信息");
		System.out.println("5.根据年龄段查询学生信息");
		Scanner sc = new Scanner(System.in);
		int j = sc.nextInt();
		if (j == 1) {
			String strSql = "select * from EDUMSTUDENTMESSAGE order by studentidentifier desc";
			List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
					new BeanListHandler<Student>(Student.class));
			printList(lst);
		}
		if (j == 2) {
			System.out.println("请输入学生编号：");
			Integer studentid = sc.nextInt();
			String strSql = "select * from EDUMSTUDENTMESSAGE where studentid=?";
			List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
					new BeanListHandler<Student>(Student.class), new Object[] { studentid });
			printList(lst);
		}
		if (j == 3) {
			System.out.println("请输入学生姓名信息");
			String studentname = sc.next();
			String strSql = "select * from EDUMSTUDENTMESSAGE where studentname like '%" + studentname + "%'";
			List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
					new BeanListHandler<Student>(Student.class));
			printList(lst);
		}
		if (j == 4) {
			System.out.println("请输入学生住址信息");
			String studentaddress = sc.next();
			String strSql = "select * from EDUMSTUDENTMESSAGE where studentaddress like '%" + studentaddress + "%'";
			List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
					new BeanListHandler<Student>(Student.class));
			printList(lst);
		}
		if (j == 5) {
			System.out.println("请输入要起始的查询年龄");
			String studentage1 = sc.next();
			System.out.println("请输入要终止的查询年龄");
			String studentage2 = sc.next();
			String strSql = "select * from EDUMSTUDENTMESSAGE where studentage between" + studentage1 + "and"
					+ studentage2;
			List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
					new BeanListHandler<Student>(Student.class));
			printList(lst);
		}

	}

	/**
	 * 学生修改密码
	 */
	public void stuUpdateStuMessage(Student stuMessage) {
		System.out.println("请输入旧密码");
		Scanner sc = new Scanner(System.in);
		String strpassword = sc.next();
		if (strpassword.equals(stuMessage.getStudentpassword())) {
			System.out.println("请输入新密码");
			String strpasswordnew = sc.next();
			String strSql="update EDUMSTUDENTMESSAGE set studentpassword=" + strpasswordnew+" where studentid="+stuMessage.getStudentid();
			DBDriverFactory.getDBDriverInstance().update(strSql);
			stuMessage.setStudentpassword(strpasswordnew);
			System.out.println("修改成功");
		} else {
			System.out.println("输入错误，请重新进入修改");
		}
	}

	/**
	 * 查询自己的信息
	 * 
	 * @param stuMessage
	 */
	public void stuSelectStuselfMessage(Student stuMessage) {

		String strSql = "select * from EDUMSTUDENTMESSAGE where studentid=" + stuMessage.getStudentid()
				+ "and studentcondition=1";
		List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<Student>(Student.class));
		printList(lst);
	}

	/**
	 * 查询同学信息
	 */
	public void stuSelectStuMessage(Student stuMessage) {
		System.out.println("选择要查询的的内容：");
		System.out.println("1.查询所有学生信息");// （查询出来的同学信息密码不可见或者使用暗文）
		System.out.println("2.根据指定编号查询单个学生信息");
		System.out.println("3.根据姓名模糊查询学生信息");
		System.out.println("4.根据地址模糊查询学生信息");
		System.out.println("5.根据年龄段查询学生信息");
		Scanner sc = new Scanner(System.in);
		// j为要查询的上述内容编号
		int j = sc.nextInt();
		if (j == 1) {
			String strSql = "select studentid,studentname,studentsex,"
					+ "studentage,studentphonenumber,studentaddress,studentqq from EDUMSTUDENTMESSAGE where studentcondition=1";
			List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
					new BeanListHandler<Student>(Student.class));
			printList(lst);
		}
		if (j == 2) {
			System.out.println("请输入要查询的学生账号");
			String strstuid = sc.next();
			String strSql = "select studentid,studentname,studentsex,"
					+ "studentage,studentphonenumber,studentaddress,studentqq from EDUMSTUDENTMESSAGE where studentid="
					+ strstuid + "and studentcondition=1";
			List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
					new BeanListHandler<Student>(Student.class));
			printList(lst);
		}
		if (j == 3) {
			System.out.println("请输入要查询的学生地址信息");
			String strstuaddress = sc.next();
			String strSql = "select studentid,studentname,studentsex,"
					+ "studentage,studentphonenumber,studentaddress,studentqq from EDUMSTUDENTMESSAGE where studentname=%"
					+ strstuaddress + "% and studentcondition=1";
			List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
					new BeanListHandler<Student>(Student.class));
			printList(lst);
		}
		if (j == 4) {
			System.out.println("请输入要查询的学生姓名信息");
			String strstuname = sc.next();
			String strSql = "select studentid,studentname,studentsex,"
					+ "studentage,studentphonenumber,studentaddress,studentqq from EDUMSTUDENTMESSAGE where studentname=%"
					+ strstuname + "% and studentcondition=1";
			List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
					new BeanListHandler<Student>(Student.class));
			printList(lst);
		}
		if (j == 5) {
			System.out.println("请输入要查询的学生的起始年龄：");
			String strstuage1 = sc.next();
			System.out.println("请输入要查询的学生的终止年龄：");
			String strstuage2 = sc.next();
			String strSql = "select studentid,studentname,studentsex,"
					+ "studentage,studentphonenumber,studentaddress,studentqq from EDUMSTUDENTMESSAGE where studentcondition=1 and studentname between"
					+ strstuage1 + "and " + strstuage2;
			List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
					new BeanListHandler<Student>(Student.class));
			printList(lst);
		}
	}

	/**
	 * 查询好友信息
	 */
	public void stuSelectStuFriendsMessage(Student stuMessage) {
		Integer MyId = stuMessage.getStudentid();
		String strSql = "select studentid,studentname,studentsex,"
				+ "studentage,studentphonenumber,studentaddress,studentqq from EDUMSTUDENTMESSAGE where studentid in(select studentfriends from EDUMFRIENDS where studentid="
				+ MyId + "and waitconfrimation=1)";
		List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<Student>(Student.class));
		printList(lst);
	}

	/**
	 * 添加好友
	 */
	public void stuAddStuFriend(Student stuMessage) {
		Scanner sc = new Scanner(System.in);
		// 本人账号
		Integer MyId = stuMessage.getStudentid();
		System.out.println("请输入要添加的好友账号：");
		// strstuid--要添加的同学账号
		String strstuid = sc.next();
		System.out.println("添加备注姓名：");
		String strstuname = sc.next();
		// 发送验证消息
		String strSql = "update EDUMSTUDENTMESSAGE set confrimationmessage=1,informationsource=" + MyId
				+ " where studentid=" + strstuid;
		String strSql1 = "insert into EDUMFriends(studentid,studentfriends,friendnote) values("+MyId+","+strstuid+",'"+strstuname+"')";
		int i = DBDriverFactory.getDBDriverInstance().update(strSql);
		int j = DBDriverFactory.getDBDriverInstance().update(strSql1);
		System.out.println("等待对方确认");

	}

	/**
	 * 处理好友验证消息
	 * 
	 * @param stuMessage
	 */
	public void confrimationMessage(Student stuMessage) {
		Scanner sc = new Scanner(System.in);
		Integer MyId = stuMessage.getStudentid();
		Integer message = stuMessage.getConfrimationmessage();
		Integer informationsource = stuMessage.getInformationsource();
		if (message == 1) {
			System.out.println("你有一条来自" + informationsource + "的好友添加消息");
			System.out.println("同意添加请输入'Y',不同意请输入'N'");
			String friendConfrimation = sc.next();
			if (friendConfrimation.equals("Y")) {
				// 添加好友
				String strSql = "insert into EDUMFRIENDS(studentid,studentfriends,waitconfrimation) values(" + MyId
						+ "," + informationsource + ",1)";
				int i = DBDriverFactory.getDBDriverInstance().update(strSql);
				String strSql2 = "update EDUMFriends set waitconfrimation=1 where studentid=" + informationsource;
				int j = DBDriverFactory.getDBDriverInstance().update(strSql2);
				if (i > 0) {
					System.out.println("好友添加成功，是否添加备注'Y'（是）'N'(否)");
					String judge = sc.next();
					if (judge.equals("Y")) {
						System.out.println("输入备注");
						String strnote = sc.next();
						String strSql1 = "update EDUMFriends set friendnote=" + strnote + " where studentid=" + MyId;
						int i1 = DBDriverFactory.getDBDriverInstance().update(strSql);
						if (i > 0) {
							System.out.println("添加备注成功");
						} else {
							System.out.println("添加失败");
						}
					}
				} else {
					System.out.println("好友添加失败失败");
				}
			}
		}
	}

	/**
	 * 查看学生账号是否存在
	 * 
	 * @param stuAccount
	 *            输入的学生账号
	 * @return true学生信息存在，false学生信息不存在
	 */
	private boolean isStuAccountExisted(String stuAccount) {
		String strSql = "select * from EDUMSTUDENTMESSAGE where studentid=?";
		List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<Student>(Student.class), new Object[] { stuAccount });
		return lst != null && lst.size() > 0 ? true : false;
	}

	/**
	 * 根据输入的账号得到数据库中的学生信息
	 * 
	 * @param stuAccount
	 *            输入的学生账号
	 * @return 查询到的该账号下的学生信息
	 */
	private List getStudentInfoByCode(String stuAccount) {
		// 查询语句
		String strSql = "SELECT * FROM EDUMSTUDENTMESSAGE WHERE studentid = ?";
		// 查询结果
		List<Student> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<Student>(Student.class), new Object[] { stuAccount });

		return lst;
	}

	/**
	 * 遍历list集合信息
	 * 
	 * @param lst
	 */
	public void printList(List lst) {
		System.out.println("list集合存储的二维表格内容：");
		for (int i = 0; i < lst.size(); i++) {
			System.out.println(lst.get(i).toString());
			// Iterator it = lst.iterator();
			// // 迭代器遍历
			// while (it.hasNext()) {
			// // 获取当前遍历的元素
			// Object obj = it.next();
			// System.out.println("\t-" + obj.toString());
			// }
		}
	}

}
